//
//  HYOUSortViewController.m
//  BestGift
//
//  Created by 温晓宇 on 15/12/30.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "HYOUSortViewController.h"
#import "HYOUMinGiftView.h"
#import "HYOUStrategyView.h"
#import "HYOUStrategyCollectionModel.h"
#import "HYOUStrategyHeadView.h"
#import "HYOUStrategyName.h"
#import "HYOUStrategyCollectionView.h"
#import "HYOUSortDetailViewController.h"
#import "HYOUTopicMoreViewController.h"
#import "HYOUTopicDetailViewController.h"
#import "HYOUGiftTargetViewController.h"
@interface HYOUSortViewController ()<HYOUStrategyViewDelegate,HYOUMinGiftViewDelegate>

@property(nonatomic, retain)HYOUMinGiftView *gift;
@property(nonatomic, retain)HYOUStrategyView *strateg;

@property(nonatomic, retain) NSMutableArray *nameGroupsArray;
@property(nonatomic, retain) NSMutableArray *categoryArray;
@property(nonatomic, retain) NSMutableArray *targetArray;
@property(nonatomic, retain) NSMutableArray *occasionArray;
@property(nonatomic, retain) NSMutableArray *styleArray;

@property(nonatomic, retain) NSMutableArray *allArray;

@end

@implementation HYOUSortViewController

-(void)dealloc{
    [_gift release];
    [_strateg release];
    [_nameGroupsArray release];
    [_categoryArray release];
    [_targetArray release];
    [_styleArray release];
    [_occasionArray release];
    [_allArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSArray *segmentArray = [NSArray arrayWithObjects:@"攻略", @"礼物", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:segmentArray];
    segment.frame = CGRectMake(WIDTH / 2, 10, 200, 30);
//    segment.tintColor = RGBA(231, 46, 67, 1);
    segment.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = segment;
    segment.selectedSegmentIndex = 0;
    [segment release];
    
    UIImage *image = [[UIImage imageNamed:@"iconfont-jycxun"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(searchAction:)];
    
    //app字体
    [self setFontFamily:@"FZLTXHK--GBK1-0" forView:self.view andSubViews:YES];
    
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    self.gift = [[HYOUMinGiftView alloc] initWithFrame:self.view.bounds];
    self.gift.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.gift];
    [_gift release];
    self.gift.delegate = self;
    
    self.strateg = [[HYOUStrategyView alloc] initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.strateg.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:self.strateg];
    [_strateg release];
    
    self.strateg.delegate = self;
    
    
    
    
    /********************大轮播图*******************/
    self.allArray = [NSMutableArray array];
    NSString *url = @"http://api.liwushuo.com/v2/collections?limit=10&offset=0";
    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = resuposeObject;
        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
        NSMutableArray *collections = [data objectForKey:@"collections"];
        for (NSDictionary *dic in collections) {
            HYOUStrategyCellModel *model = [[HYOUStrategyCellModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
            [model release];
        }
        self.strateg.allArray = self.allArray;
//        NSLog(@"%ld",self.allArray.count);
    }];
    
    /***********分类*****************/
    self.nameGroupsArray = [NSMutableArray array];
    self.categoryArray = [NSMutableArray array];
    self.targetArray = [NSMutableArray array];
    self.occasionArray = [NSMutableArray array];
    self.styleArray = [NSMutableArray array];
    NSString *iconUrl = @"http://api.liwushuo.com/v2/channel_groups/all";
    [AppTools finalAFNWithURLString:iconUrl parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = [resuposeObject objectForKey:@"data"];
        NSMutableArray *channelArr = [bigDic objectForKey:@"channel_groups"];
        for (NSMutableDictionary *dic in channelArr) {
            HYOUStrategyName *typeGroupModel = [[HYOUStrategyName alloc] init];
            [typeGroupModel setValuesForKeysWithDictionary:dic];
            [self.nameGroupsArray addObject:typeGroupModel];
            
            if ([[dic objectForKey:@"name" ]isEqualToString:@"品类"]) {
                NSMutableArray *array = [dic objectForKey:@"channels"];
                for (NSMutableDictionary *dic in array) {
                    HYOUStrategyCollectionModel *channelsModel = [[HYOUStrategyCollectionModel alloc] init];
                    [channelsModel setValuesForKeysWithDictionary:dic];
                    [self.categoryArray addObject:channelsModel];
                }
            }
            if ([[dic objectForKey:@"name"] isEqualToString:@"对象"]) {
                NSMutableArray *array = [dic objectForKey:@"channels"];
                for (NSMutableDictionary *dic in array) {
                    HYOUStrategyCollectionModel *channelsModel = [[HYOUStrategyCollectionModel alloc] init];
                    [channelsModel setValuesForKeysWithDictionary:dic];
                    [self.targetArray addObject:channelsModel];
                }
            }
            if ([[dic objectForKey:@"name"] isEqualToString:@"场合"]) {
                NSMutableArray *array = [dic objectForKey:@"channels"];
                for (NSMutableDictionary *dic in array) {
                    HYOUStrategyCollectionModel *channelsModel = [[HYOUStrategyCollectionModel alloc] init];
                    [channelsModel setValuesForKeysWithDictionary:dic];
                    [self.occasionArray addObject:channelsModel];
                }
            }
            if ([[dic objectForKey:@"name"] isEqualToString:@"风格"]) {
                NSMutableArray *array = [dic objectForKey:@"channels"];
                for (NSMutableDictionary *dic in array) {
                    HYOUStrategyCollectionModel *channelsModel = [[HYOUStrategyCollectionModel alloc] init];
                    [channelsModel setValuesForKeysWithDictionary:dic];
                    [self.styleArray addObject:channelsModel];
                }
            }
        }
        self.strateg.nameGroupsArray = self.nameGroupsArray;
        self.strateg.categoryArray = self.categoryArray;
        self.strateg.targetArray = self.targetArray;
        self.strateg.occasionArray = self.occasionArray;
        self.strateg.styleArray = self.styleArray;
        
    }];
    
}
-(void)showSeeMore{
    HYOUTopicMoreViewController *topic = [[HYOUTopicMoreViewController alloc] init];
    [self.navigationController pushViewController:topic animated:YES];
}

-(void)showDetail:(NSString *)url{
    HYOUSortDetailViewController *detail = [[HYOUSortDetailViewController alloc] init];
    detail.url = url;
    [self.navigationController pushViewController:detail animated:YES];
}
-(void)showTopic:(NSString *)url{
    HYOUTopicDetailViewController *detail = [[HYOUTopicDetailViewController alloc] init];
    detail.url = url;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)segmentAction:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.strateg];
    }
    if (segment.selectedSegmentIndex == 1) {
        [self.view bringSubviewToFront:self.gift];
    }
}
-(void)searchAction:(UIBarButtonItem *)btn{
    HYOUSearchViewController *search = [[HYOUSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
//字体实现
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
    if ([view isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)view;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
    }
    if (isSubViews)
    {
        for (UIView *sview in view.subviews)
        {
            [self setFontFamily:fontFamily forView:sview andSubViews:YES];
        }
    }
}
-(void)getGiftMore:(NSString *)url{
    HYOUGiftTargetViewController *target = [[HYOUGiftTargetViewController alloc]init];
    target.url = url;
    [self.navigationController pushViewController:target animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
